package com.surelution.projects.crm

import java.text.SimpleDateFormat

class ImportExcelController {

    static allowedMethods = [save: "POST", update: "POST", delete: "POST"]
	def ImportExcelService
	
    def index = {
        redirect(action: "list", params: params)
    }
	
	def downloadFile = {
		def file = ImportExcel.get(params.id)
		def filePath = ImportExcelService.loadFile(file)
		response.setHeader("Content-Disposition",
			"attachment;filename=${file.fileName}");
		
		FileInputStream fos = new FileInputStream(filePath)
		response.outputStream << fos
		response.outputStream.flush()
		fos.close()
		return
	}
	
	def showFile = {
		def file = ImportExcel.get(params.id)
		def filePath = ImportExcelService.loadFile(file)
		
		FileInputStream fos = new FileInputStream(filePath)
		response.outputStream << fos
		response.outputStream.flush()
		fos.close()
		return
	}
	
    def list = {
        params.max = Math.min(params.max ? params.int('max') : 10, 100)
		def sdf = new SimpleDateFormat('yyyy-MM-dd')
		def dateFrom = params.dateFrom? sdf.parse(params.dateFrom):null
		def dateTo = params.dateTo? sdf.parse(params.dateTo):null
		def type = params.type? params.type:null
		String sto = '''select distinct(type) from ImportExcel ie'''
		def typeList = ImportExcel.executeQuery(sto.toString())
		def result = ImportExcel.createCriteria().list ([max:params.max, offset:params.offset]){
			if(type){
				eq("type", type)
			}
			if(dateFrom) {
				ge("importTime", dateFrom)
			}
			if(dateTo) {
				le("importTime", dateTo)
			}
		}
        [importExcelInstanceList: result, importExcelInstanceTotal: result.totalCount,typeList:typeList]
    }

    def create = {
        def importExcelInstance = new ImportExcel()
        importExcelInstance.properties = params
        return [importExcelInstance: importExcelInstance]
    }

    def save = {
        def importExcelInstance = new ImportExcel(params)
        if (importExcelInstance.save(flush: true)) {
            flash.message = "${message(code: 'default.created.message', args: [message(code: 'importExcel.label', default: 'ImportExcel'), importExcelInstance.id])}"
            redirect(action: "show", id: importExcelInstance.id)
        }
        else {
            render(view: "create", model: [importExcelInstance: importExcelInstance])
        }
    }

    def show = {
        def importExcelInstance = ImportExcel.get(params.id)
        if (!importExcelInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'importExcel.label', default: 'ImportExcel'), params.id])}"
            redirect(action: "list")
        }
        else {
            [importExcelInstance: importExcelInstance]
        }
    }

    def edit = {
        def importExcelInstance = ImportExcel.get(params.id)
        if (!importExcelInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'importExcel.label', default: 'ImportExcel'), params.id])}"
            redirect(action: "list")
        }
        else {
            return [importExcelInstance: importExcelInstance]
        }
    }

    def update = {
        def importExcelInstance = ImportExcel.get(params.id)
        if (importExcelInstance) {
            if (params.version) {
                def version = params.version.toLong()
                if (importExcelInstance.version > version) {
                    
                    importExcelInstance.errors.rejectValue("version", "default.optimistic.locking.failure", [message(code: 'importExcel.label', default: 'ImportExcel')] as Object[], "Another user has updated this ImportExcel while you were editing")
                    render(view: "edit", model: [importExcelInstance: importExcelInstance])
                    return
                }
            }
            importExcelInstance.properties = params
            if (!importExcelInstance.hasErrors() && importExcelInstance.save(flush: true)) {
                flash.message = "${message(code: 'default.updated.message', args: [message(code: 'importExcel.label', default: 'ImportExcel'), importExcelInstance.id])}"
                redirect(action: "show", id: importExcelInstance.id)
            }
            else {
                render(view: "edit", model: [importExcelInstance: importExcelInstance])
            }
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'importExcel.label', default: 'ImportExcel'), params.id])}"
            redirect(action: "list")
        }
    }

    def delete = {
        def importExcelInstance = ImportExcel.get(params.id)
        if (importExcelInstance) {
            try {
                importExcelInstance.delete(flush: true)
                flash.message = "${message(code: 'default.deleted.message', args: [message(code: 'importExcel.label', default: 'ImportExcel'), params.id])}"
                redirect(action: "list")
            }
            catch (org.springframework.dao.DataIntegrityViolationException e) {
                flash.message = "${message(code: 'default.not.deleted.message', args: [message(code: 'importExcel.label', default: 'ImportExcel'), params.id])}"
                redirect(action: "show", id: params.id)
            }
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'importExcel.label', default: 'ImportExcel'), params.id])}"
            redirect(action: "list")
        }
    }
}
